package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.CaseMemberApiService;
import com.yixun.qfbao.api.service.V10BindService;
import com.yixun.qfbao.apidto.v10dto.BaseResponseDto;
import com.yixun.qfbao.model.OtherCompany;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.PremisesRelationExternal;
import com.yixun.qfbao.model.dto.PremisesRelationExternalDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.ExternalSysEnum;
import com.yixun.qfbao.dao.OtherCompanyDao;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.service.PremisesRelationExternalService;
import com.yixun.qfbao.xf.model.CaseMember;
import com.yixun.qfbao.xf.model.Premises;
import com.yixun.qfbao.xf.model.dto.CaseMemberDto;
import com.yixun.qfbao.xf.model.enums.PremisesStatusEnum;
import com.yixun.qfbao.xf.service.CaseMemberService;
import com.yixun.qfbao.xf.service.PremisesService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CaseMemberApiServiceImpl implements CaseMemberApiService{
    @Autowired
    private CaseMemberService caseMemberService;
    @Autowired
    private PremisesService premisesService;
    @Autowired
    private OtherCompanyDao otherCompanyDao;
    @Autowired
    private V10BindService v10BindService;
    @Autowired
    private PremisesRelationExternalService premisesRelationExternalService;
    @Autowired
    private PartnerInfoService partnerInfoService;

    /**
     * 更新
     * @param premisesId
     * @param caseMemberId
     * @return
     */
    public int setCaseHotline(Long premisesId,Long caseMemberId){
        CaseMemberDto caseMember = new CaseMemberDto();
        caseMember.setPhoneStatus("0");
        caseMember.setPremisesId(premisesId);
        caseMemberService.updateForPhoneStatus(caseMember);

        caseMember.setPhoneStatus("1");
        caseMember.setId(caseMemberId);
        caseMemberService.update(caseMember);

        //推送楼盘到v10
        Premises premises = premisesService.selectById(premisesId);
        //获取对接人信息
        CaseMember caseMemberPartnerInfo = caseMemberService.selectById(caseMemberId);
        PartnerInfo partnerInfo = partnerInfoService.selectById(caseMemberPartnerInfo.getPartnerId());
        if(partnerInfo != null){
            premises.setManager(partnerInfo.getAccountName());
            premises.setMobile(partnerInfo.getPhone());
        }
        //审核通过的楼盘
        if(!PremisesStatusEnum.type1.getNo().equals(premises.getPremisesStatus())){
            return 0;
        }
        List<OtherCompany> listOtherCompany = otherCompanyDao.getV10CompanyListByCompanyId(premises.getCompanyId());

        if(listOtherCompany == null || listOtherCompany.size() == 0){
            return 0;
        }

        // 获取所需推送状态的楼盘
        PremisesRelationExternalDto preDTO = new PremisesRelationExternalDto();
        preDTO.setSysId(ExternalSysEnum.V10.getCode());
        preDTO.setIsDeleted(DelFlagEnum.UNDEL.getNo());
        preDTO.setPremisesId(premises.getId());
        List<PremisesRelationExternal> list = premisesRelationExternalService.selectListByDto(preDTO);
        if (list != null && list.size() > 0) {
            List<BaseResponseDto> responseDtoList =  v10BindService.saveFirsthandEstate(premises,listOtherCompany);
            if(CollectionUtils.isNotEmpty(responseDtoList)){
                return responseDtoList.size();
            }
        }

        return 0;

    }

    public int unSetCaseHotline(Long cmsId){
        CaseMember cm = caseMemberService.selectById(cmsId);

        if( cm != null &&"1".equals(cm.getPhoneStatus())){
            Premises premises = premisesService.selectById(cm.getPremisesId());
            List<OtherCompany> listOtherCompany = otherCompanyDao.getV10CompanyListByCompanyId(premises.getCompanyId());

            if(listOtherCompany == null || listOtherCompany.size() == 0){
                return 0;
            }

            // 获取所需推送状态的楼盘
            PremisesRelationExternalDto preDTO = new PremisesRelationExternalDto();
            preDTO.setSysId(ExternalSysEnum.V10.getCode());
            preDTO.setIsDeleted(DelFlagEnum.UNDEL.getNo());
            preDTO.setPremisesId(premises.getId());
            List<PremisesRelationExternal> list = premisesRelationExternalService.selectListByDto(preDTO);
            if (list != null && list.size() > 0) {
                List<BaseResponseDto> responseDtoList =  v10BindService.saveFirsthandEstate(premises,listOtherCompany);
            }
        }

        if(cm != null){
            CaseMemberDto cmDto = new CaseMemberDto();
            cmDto.setId(cmsId);
            cmDto.setDelFlag(DelFlagEnum.DEL.getNo());
            caseMemberService.update(cmDto);
        }
        return 0;
    }
}
